using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using SouthernLand.Common.BOL.NhaDat;

namespace SouthernLand.Common.DAL.NhaDat
{
    public class LoaitinDAL : BaseDAL
    {
        #region ObjIDataReader

        /// <summary>
        /// Returns a new Loaitin instance filled with the DataReader's current record data 
        /// </summary>
        private Loaitin GetLoaitinFromReader(IDataReader dr)
        {
            var obj = new Loaitin();
            obj.LoaitinID = (dr["LoaitinID"] is DBNull) ? Int32.MinValue : (Int32) dr["LoaitinID"];
            obj.TenLoaitin = (dr["TenLoaitin"] is DBNull) ? String.Empty : (String) dr["TenLoaitin"];
            obj.TenLoaitin_en = (dr["TenLoaitin_en"] is DBNull) ? String.Empty : (String) dr["TenLoaitin_en"];
            return obj;
        }

        #endregion

        /// <summary>
        /// Returns a collection of Loaitin with the data read from the input DataReader
        /// </summary>
        private List<Loaitin> GetLoaitinCollectionFromReader(IDataReader dr)
        {
            var obj = new List<Loaitin>();
            while (dr.Read())
                obj.Add(GetLoaitinFromReader(dr));
            return obj;
        }

        public int Insert(Loaitin obj)
        {
            using (SqlConnection myConn = GetConnectionString())
            {
                var myCommand = new SqlCommand("w4u_Loaitin_Insert", myConn);
                myCommand.CommandType = CommandType.StoredProcedure;
                myCommand.Parameters.Add("@LoaitinID", SqlDbType.Int).Value = obj.LoaitinID;
                myCommand.Parameters.Add("@TenLoaitin", SqlDbType.NVarChar).Value = obj.TenLoaitin;
                myCommand.Parameters.Add("@TenLoaitin_en", SqlDbType.NVarChar).Value = obj.TenLoaitin_en;

                return myCommand.ExecuteNonQuery();
            }
        }

        public bool Update(Loaitin obj)
        {
            using (SqlConnection myConn = GetConnectionString())
            {
                var myCommand = new SqlCommand("w4u_Loaitin_Update", myConn);
                myCommand.CommandType = CommandType.StoredProcedure;
                myCommand.Parameters.Add("@LoaitinID", SqlDbType.Int).Value = obj.LoaitinID;
                myCommand.Parameters.Add("@TenLoaitin", SqlDbType.NVarChar).Value = obj.TenLoaitin;
                myCommand.Parameters.Add("@TenLoaitin_en", SqlDbType.NVarChar).Value = obj.TenLoaitin_en;

                int ret = myCommand.ExecuteNonQuery();
                return (ret == 1);
            }
        }

        public bool Delete(Int32 LoaitinID)
        {
            using (SqlConnection myConn = GetConnectionString())
            {
                var myCommand = new SqlCommand("w4u_Loaitin_Delete", myConn);
                myCommand.CommandType = CommandType.StoredProcedure;
                myCommand.Parameters.Add("@LoaitinID", SqlDbType.Int).Value = LoaitinID;

                int ret = myCommand.ExecuteNonQuery();
                return (ret == 1);
            }
        }

        /// <summary>
        /// Retrieves the w4u_Loaitin with the specified ID
        /// </summary>
        public Loaitin GetLoaitinByID(Int32 LoaitinID)
        {
            using (SqlConnection myConn = GetConnectionString())
            {
                var myCommand = new SqlCommand("w4u_Loaitin_GetLoaitinByID", myConn);
                myCommand.CommandType = CommandType.StoredProcedure;
                myCommand.Parameters.Add("@LoaitinID", SqlDbType.Int).Value = LoaitinID;

                SqlDataReader dr = myCommand.ExecuteReader();
                return (dr.Read() ? GetLoaitinFromReader(dr) : null);
            }
        }

        //Get All Loaitin
        public List<Loaitin> GetAllLoaitin(int PageIndex, int PageSize)
        {
            using (SqlConnection myConn = GetConnectionString())
            {
                var myCommand = new SqlCommand("w4u_Loaitin_GetAllLoaitin", myConn);
                myCommand.CommandType = CommandType.StoredProcedure;
                myCommand.Parameters.Add("@PageIndex", SqlDbType.Int).Value = PageIndex;
                myCommand.Parameters.Add("@PageSize", SqlDbType.Int).Value = PageSize;
                SqlDataReader dr = myCommand.ExecuteReader();
                return GetLoaitinCollectionFromReader(dr);
            }
        }

        // Get Count All records of Table  Loaitin
        public int GetCountLoaitin()
        {
            using (SqlConnection myConn = GetConnectionString())
            {
                var myCommand = new SqlCommand("w4u_Loaitin_GetCountLoaitin", myConn);
                myCommand.CommandType = CommandType.StoredProcedure;
                return (int) myCommand.ExecuteScalar();
            }
        }
    }
}